/**
 * 料号与客户品号对应关系 - 编辑页面
 * @author shixc
 * @date 2018/01/16
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            form: '',
            rules: {
                GWEB002: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWEB004: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWEB007: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWEB011: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWEBPLANT: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ]
            },
            selectOptions: {
                sys_y_n: app.datas_("sys_y_n"),
                options1: [],
                options2: []
            }
        },
        callObj,
        pageVue
    ;

    var Gwei002Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwei002_edit',
                _loadTplsFull: [
                    { dom: 'common_open_window', tpl: 'common/_open_window'},
                ]
            }, win);
        },
        edit: function () {
            app.ajax({
                xurl: 'crm/gwei002/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: '料号与客户零件信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        //callObj.update(result.xdata);
                        app.pageModule('gwei002').reloadTableCurr();
                        layer.close(callObj.layOpenIndex);
                    }
                }
            });
        },
        add: function () {
            app.ajax({
                xurl: 'crm/gwei002/add',
                xdata: vueData.form,
                loading: true,
                okmsg: '料号与客户零件信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwei002').reloadTableCurr();
                        layer.close(callObj.layOpenIndex);
                    }
                }
            });
        },
        _call: function (obj) {
            pageVue.$refs['form'].resetFields();
            callObj = obj;
            if (obj.is_add) {
                vueData.isAdd = true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        GWEB011: 'Y'
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['GWEB']);
            pageVue = new Vue({
                el: '#gwei002_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        layer.close(callObj.layOpenIndex);
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.add();
                                } else {
                                    self.edit();
                                }
                            }
                        });
                    },
                    change: function (val) {
                        var obj = {};
                        obj = vueData.selectOptions.options1.find(function (item) {
                            return item.GWAK001 === val
                        });
                        vueData.form.GWEB003 = obj.GWAKL003;
                    },
                    openKh:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config:{
                                    method:'q_gwak001',
                                    columnList:[
                                        {label:'客户编号',prop:'GWAK001',width:150},
                                        {label:'客户简称',prop:'GWAKL003',width:150},
                                        {label:'客户全称',prop:'GWAKL004',width:150}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWEB002 = data.GWAK001;
                                    vueData.form.GWEB003 = data.GWAKL003;
                                },
                            },
                            index: 0,
                            openOpts: {
                                title: '选择客户信息',
                                area: ['700px', '430px']
                            }
                        });
                    },
                    openGWEB004:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config:{
                                    method:'q_gwam001',
                                    columnList:[
                                        {label:'设备编号',prop:'GWAM001', width: 150},
                                        {label:'品名',prop:'GWAML003', width: 150},
                                        {label:'规格',prop:'GWAML004', width: 150}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWEB004 = data.GWAM001;
                                    vueData.form.GWEB005 = data.GWAML003;
                                    vueData.form.GWEB006 = data.GWAML004;
                                },
                            },
                            index: 0,
                            openOpts: {
                                title: '选择产品信息',
                                area: ['700px', '430px']
                            }
                        });
                    },
                    clearGweb002:function () {
                        if(vueData.form.GWEB002 == '' || vueData.form.GWEB002 == null ){
                            vueData.form.GWEB003 = '';
                        }
                    },
                    clearGweb004:function () {
                        if(vueData.form.GWEB004 == '' || vueData.form.GWEB004 == null ){
                            vueData.form.GWEB005 = '';
                            vueData.form.GWEB006 = '';
                        }
                    }
                },
                created: function () {
                    self._setEleIsReady('gwei002_edit');
                }
            });
        },
        _lazy: function () {
            /*app.ajax({
                xurl: 'crm/gwei002/getGwakList',
                cb: function (result) {
                    vueData.selectOptions.options1 = result.data.list;
                }
            });*/
            app.ajax({
                xurl: 'crm/gwei002/getOrgList',
                cb: function (result) {
                    vueData.selectOptions.options2 = result.data.list;
                }
            });
            this._super();
        }
    });

    return new Gwei002Edit();

});
